(function($, undefined ) {
    App = Ember.Application.create();
    App.store = DS.Store.create({
        revision: 11,
        adapter: DS.DjangoTastypieAdapter.extend()
    });

    var attr = DS.attr;

    App.Sector = DS.Model.extend({
        sector: attr('string'),
        poster: DS.belongsTo('App.User'),
        posted_date: attr('date')
    });
    App.User = DS.Model.extend({
        username: attr('string')
    });

    App.Router.map(function() {
        this.route('sectors-list');
        this.route('about');
        this.route('contact');
        this.route('add-sector');
    });

    App.IndexRoute = Ember.Route.extend({
        redirect: function() {
            this.transitionTo('sectors-list');
        }
    });
    App.SectorsListRoute = Ember.Route.extend({
        setupController: function(controller) {
            this._super();
            controller.set('sectors', App.Sector.find());
        }
    });

    App.AddSectorController = Em.Controller.extend({
        sector: '',
        saveSector: function(text) {
            if (text) {
                var sector = App.Sector.createRecord({'sector': text});
                sector.store.commit();
                this.set('sector', '');
                this.transitionToRoute('sectors-list');
            }
        }
    });

    App.AddSectorView = Ember.View.extend({
        submit: function() {
            var text = this.get('controller.sector');
            this.get('controller').send('saveSector', text);
        }
    });
}(jQuery));
